#include "execute_task.h"
TaskHandle_t executeHandle;
// 配置数据
THData thData;
// 处理完成的数据
extern GatherTargetData targetData;
void showContrl();
void alarmContrl();
void airContrl();
void curtainContrl();
// 执行任务
void executeTask(void* param)
{
    while (1)
    {
        if (thData.state & 0x01)
        {
            showContrl();
        }
        if (thData.state & 0x02)
        {
            alarmContrl();
        }
        if (thData.state & 0x04)
        {
            airContrl();
        }
        if (thData.state & 0x08)
        {
            curtainContrl();
        }
        vTaskDelay(500);
    }
}
// 执行初始化
void execute_init()
{
    // 读取配置默认值 暂时写死
    if (thData.state == 0)
    {
        thData.state = 15; //1111
		thData.tempMax = 200;
		thData.tempMin = -20;
		thData.humiMax = 200;
		thData.humiMin = 20;
		thData.sunshine = 100;
		thData.smoke = 200;
    }

    // 外设初始化
    BEEP_Init();
    OLED_Init();

}
// 创建执行任务
void createExecuteTask()
{
    xTaskCreate(
        executeTask,
        "executeTask",
        128,
        NULL,
        2,
        &executeHandle
    );
}
void showContrl()
{
    OLED_Clear();
	OLED_ShowString(0,0,  "Temp:  C",12);
	OLED_ShowString(0,15, "Humi:  %",12);
	OLED_Refresh_Gram();
    OLED_ShowNum(5*6,0,targetData.temp,2,12);
	OLED_ShowNum(5*6,15,targetData.humi,2,12);
    OLED_Refresh_Gram();
}
void alarmContrl()
{
    u8 beepFlag = 0;
    if (targetData.temp > thData.tempMax || targetData.temp < thData.tempMin)
    {
        beepFlag = 1;
        LED1 = 0;
    }
    else
    {
        beepFlag = 0;
        LED1 = 1;
    }
    if (targetData.humi > thData.humiMax || targetData.humi < thData.humiMin)
    {
        beepFlag = 1;
        LED2 = 0;
    }
    else
    {
        beepFlag = 0;
        LED2 = 1;
    }
    if (targetData.smoke > thData.smoke)
    {
        beepFlag = 1;
        LED3 = 0;
    }
    else
    {
        beepFlag = 0;
        LED3 = 1;
    }
    if (targetData.sunshine > thData.sunshine)
    {
        beepFlag = 1;
        LED4 = 0;
    }
    else
    {
        beepFlag = 0;
        LED4 = 1;
    }
    if (!targetData.raidrop)
    {
        beepFlag = 1;
        LED5 = 0;
    }
    else
    {
        beepFlag = 0;
        LED5 = 1;
    }

    // 启动蜂鸣器
    if (beepFlag)
    {
        // while (1)
        // {
        //     BEEP = !BEEP;
        //     delay_us(10);
        // }
    }
}
void airContrl()
{

}
void curtainContrl()
{

}